//
//  ConsultViewController.m
//  MagazineShangejie
//
//  Created by wen on 15/9/20.
//  Copyright © 2015年 CnbizMedia. All rights reserved.
//

#import "ConsultViewController.h"
#import "Util.h"
@interface ConsultViewController (){

    NSDictionary *wordDic;
    NSURL *shareURL;
    UITableViewCell *cell ;
    NSMutableArray *titleArray;
    
     NSMutableArray*userData;
    
    NSString *getString;
    NSString *idString;
    NSString *catIdString;
    
    NSNumber *company;
    
    NSDictionary *HeadDic;
    
    NSDictionary *bodyDic;
    NSMutableArray *anliArray;
    
}
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UITableView *zhiXunTableView;

@end

@implementation ConsultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    NSString *whereComeForm = [UserDefaults objectForKey:@"whereComeForm"];
//    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
//    if ([newOldYear isEqualToString:@"NewYear"]) {
//        //状态栏的文字颜色换成白色
//        //        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//        
//        if ([whereComeForm isEqualToString:@"clube"]) {
//            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
//        }
//    }
    
    [_tuiJianButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    anliArray = [[NSMutableArray alloc] init];
    
    //2. 取出数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger number = [userDefault integerForKey:@"buttonTagNumber"];
    
    [SVProgressHUD showWithStatus:@"数据加载中..." maskType:SVProgressHUDMaskTypeClear];
    
    switch (number) {
        case 0:
            self.title = @"论坛";
            break;
            
        case 1:
            self.title = @"游学";
            break;
            
        case 2:
            self.title = @"咨询";
            break;
            
        case 3:
            self.title = @"创投";
            break;
            
        default:
            break;
    }

    
    
    NSDictionary *lunTanDic = [userDefault objectForKey:@"lunTanDic"];

    
    NSDictionary*parameter;
    parameter=@{@"slug":@"sjzixun",@"p":@"1"};//slug=luntan&p=1
    
 

    
    
    UIButton *buttonf=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonf setFrame:CGRectMake(0, 0, 22, 22)];
    [buttonf addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    [buttonf setImage:[UIImage imageNamed:@"dismiss"] forState:UIControlStateNormal];
    
    NSString *whereComeForm = [UserDefaults objectForKey:@"whereComeForm"];
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        //状态栏的文字颜色换成白色
        //        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
        if ([whereComeForm isEqualToString:@"clube"]) {
            //            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
            [buttonf setImage:[UIImage imageNamed:@"dismissGray"] forState:UIControlStateNormal];
        }
        
    }
    
    
     UIBarButtonItem*leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:buttonf];
    self.navigationItem.leftBarButtonItem=leftBarItem;
    
    getString = @"zhanluezx";
    [self getHeadDate];
    
    
    
    ////添加返回手势
    //    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    //    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    //    [[self view] addGestureRecognizer:recognizer];

}


- (void)getHeadDate{
//slug=zhanluezx
     NSDictionary *parameter=@{@"slug": getString};
    
    [[AppAPIClient sharedClient] GET:@"Club/List" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *result = responseObject[@"data"];
        NSLog(@"luntan-data == %@",result);
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        if([result isKindOfClass:[NSNull class]] || [result count] == 0||[result[@"content"] count]==0){
            return;
        }
        
        HeadDic = [[NSDictionary alloc] init ];
 
        NSArray *dateArray = [[NSArray alloc] init];
        dateArray = result[@"content"];
        
        int i = 0;
        for (HeadDic in dateArray) {
            
            if ([HeadDic [@"listorder"] isEqualToString:@"1"]) {
                
                idString = HeadDic[@"id"];
                company  = HeadDic[@"count"];
                catIdString = HeadDic[@"catid"];
                
                [self getDatezixunanli];
                
                [self.zhiXunTableView reloadData];
                
                
                _zhiXunTableView.separatorStyle = NO;

                
                return;
            }
        }
        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


- (void)getDatezixunanli{
//slug=zixunanli
    
    NSDictionary *parameter=@{@"slug": @"zixunanli"};
    
    [[AppAPIClient sharedClient] GET:@"Club/List" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *result = responseObject[@"data"];
        NSLog(@"luntan-data == %@",result);
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        if([result isKindOfClass:[NSNull class]] || [result count] == 0||[result[@"content"] count]==0){
            return;
        }
        
        NSArray *dateArray = [[NSArray alloc] initWithArray:result[@"content"]];
 

        
        NSDictionary *chacke = [[NSDictionary alloc] init];
        int i = 0;
        for (chacke in dateArray) {
            i ++;
            NSLog(@"[chacke%@",chacke[@"comid" ]);
            NSLog(@"idString == %@",idString);
            if ([chacke[@"comid" ] isEqualToString:idString]) {
                
                [anliArray addObject:chacke];
                
                
            }
        }
        
//        bodyDic = chacke;
        
        [self.zhiXunTableView reloadData];
        
        
        _zhiXunTableView.separatorStyle = NO;
        
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    

}


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        CATransition*transition=[CATransition animation];
        transition.duration=1;
        transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type=kCATransitionPush;
        transition.subtype=kCATransitionFromLeft;//方向
        [self.view.layer addAnimation:transition forKey:nil];
        [self popOfDismss];
        //        [self dismissViewControllerAnimated:YES completion:^{
        [SVProgressHUD dismiss];
        //        }];
        
    }
}

-(void)popOfDismss{
    
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//    NSInteger number = [userDefault integerForKey:@"underBarShow"];
//    
//    if (number == 1) {
        //设置通知，显示tabbar
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showBootomBar" object:self userInfo:nil ];

//    }
    
    if ([[self.navigationController viewControllers] count]>=2) {
//        self.hidesBottomBarWhenPushed = NO;
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
//        self.hidesBottomBarWhenPushed = NO;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - Table view data source
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //    if (section==0) {
    //        return [self tableHeader];
    //    }else{
    return nil;
    //    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }
    
    else if (section == 1){
        
        return anliArray.count;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wordCell" forIndexPath:indexPath];
    ////    = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    //
    
    if (indexPath.section == 0) {
        return 187 + 45;
    }
    
    if (indexPath.section == 1) {
        return  228;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"headImageCell" forIndexPath:indexPath];
        
        if (cell==nil) {
            
            cell=[tableView dequeueReusableCellWithIdentifier:@"headImageCell"];
            
        }
        
        
        UIImageView *headImage = (UIImageView *)[cell viewWithTag:11];
        [headImage sd_setImageWithURL:[NSURL URLWithString:HeadDic[@"thumb"]]];

        UILabel *comply = (UILabel *)[cell viewWithTag:1234];
        comply.text = [NSString stringWithFormat:@"%@", HeadDic[@"count"] ];
        
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
        return cell;
    }
    
    
    if (indexPath.section == 1) {
        
    
        cell = [tableView dequeueReusableCellWithIdentifier:@"bigImageCell" forIndexPath:indexPath];
        
        if (cell==nil) {
            
            cell=[tableView dequeueReusableCellWithIdentifier:@"bigImageCell"];
            
        }
        
        NSDictionary *informationDic = anliArray[indexPath.row];
        NSLog(@"bodyDic = =%@",bodyDic);
//        userData[indexPath.row];
    UIImageView *projectImage = (UIImageView *)[cell.contentView viewWithTag:11];
    [projectImage sd_setImageWithURL:[NSURL URLWithString:informationDic[@"thumb"]]];
    
    
    UILabel*label1=(UILabel*)[cell.contentView viewWithTag:22];
        label1.text = @"《中国好声音》幕后老板。。。";
        label1.text = informationDic[@"title"];
        
//        UILabel*label2=(UILabel*)[cell.contentView viewWithTag:33];
//        label2.text = @"所在地区：";
//        label2.text = [NSString stringWithFormat: @"所在地区：%@", informationDic [@"description"]];
    
        NSString *timeString = informationDic[@"inputtime"];
        NSLog(@"timeString -- %@",timeString);
        //得到运动时间，需要转成年月日
        NSString *formatString = @"MM-dd";
        
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:formatString];
        NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeString intValue] - 28800];
        NSString *dateString =[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:confromTimesp] ];
        NSLog(@"date: %@", dateString);
    
    
        UILabel*label3=(UILabel*)[cell.contentView viewWithTag:33];
        label3.text = @"服务时间：";
        label3.text = dateString;
    
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
        
        return cell;
        }
    
    return nil;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    indexPath.row;
    
    if (indexPath.section == 1) {
    NSDictionary *informationDic = anliArray[indexPath.row];

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:informationDic forKey:@"lunTanDic"];
    [userDefault synchronize];//存入数据，单例
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    
    
//    [self performSegueWithIdentifier:@"goToZiXun" sender:indexPath];// pushShowZiXunView
        
        
    self.hidesBottomBarWhenPushed = YES;
    UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *MainCell=[story instantiateViewControllerWithIdentifier:@"OneClubeViewController"];
    [self.navigationController pushViewController:MainCell animated:YES];

        
        
    }
    //    NSDictionary *lunTanDic = userData[indexPath.row];
    //    NSLog(@"lunTanDic == %@",lunTanDic);
    //
    //    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //    [userDefault setObject:lunTanDic forKey:@"lunTanDic"];
    //    [userDefault synchronize];//存入文件
    //
    //
    //    if ([self.title isEqualToString:@"论坛"]) {
    //        [self performSegueWithIdentifier:@"IWanIdentifier" sender:indexPath];
    //    }
    
    
}





 

//新加的方法
//点击订阅及我的订阅按钮的事件效果
- (IBAction)tuiJianButtonEvent:(id)sender {
    [_tuiJianButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
//    self.meTableView.hidden=YES;
    
    [UIView animateWithDuration:0.3 animations:^(void){_redLineView.frame = CGRectMake(_tuiJianButton.frame.origin.x, _redLineView.frame.origin.y, _redLineView.frame.size.width, _redLineView.frame.size.height);} completion:^(BOOL finished) {
        _redLineView.alpha = 1;
    }];
}

- (IBAction)myButtonEvent:(id)sender {
    [_tuiJianButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
//    self.meTableView.hidden=NO;
//    [self doneLoadingViewData];
    
    [UIView animateWithDuration:0.3 animations:^(void){_redLineView.frame = CGRectMake(_myButton.frame.origin.x, _redLineView.frame.origin.y, _redLineView.frame.size.width, _redLineView.frame.size.height);} completion:^(BOOL finished) {
        _redLineView.alpha = 1;
    }];
    
}



//战略咨询
- (IBAction)zanlue:(id)sender {
    
    [SVProgressHUD showWithStatus:@"数据加载中..." maskType:SVProgressHUDMaskTypeClear];
    
    [anliArray removeAllObjects];
    getString = @"zhanluezx";
    [self getHeadDate];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setInteger:4 forKey:@"buttonTagNumber"];
    [userDefault synchronize];//存入文件
}

//招商咨询
- (IBAction)zaoShang:(id)sender {
    
    [SVProgressHUD showWithStatus:@"数据加载中..." maskType:SVProgressHUDMaskTypeClear];
    
    [anliArray removeAllObjects];
    getString = @"zhaoshangzx";
    [self getHeadDate];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setInteger:5 forKey:@"buttonTagNumber"];
    [userDefault synchronize];//存入文件

}









@end
